package com.myjob.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.myjob.config.Constants;
import com.myjob.entity.Job;
import com.myjob.entity.User;
import com.myjob.exception.OptionException;
import com.myjob.response.Result;
import com.myjob.service.JobService;
import com.myjob.service.UserService;
import com.myjob.utils.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author Chen Yuan
 * @create 2021-01-27  17:10
 */
@RestController
@Slf4j
@RequestMapping("/jobs")
public class JobController {
	@Autowired
	private JobService jobService;
	@Autowired
	private UserService userService;


	/**
	 * 查询岗位
	 * @param currentPage 当前页
	 * @param pageSize 页大小
	 * @param jobName 工作名字
	 * @param hrName 人事名字
	 * @param salaryMin 最低工资
	 * @param salaryMax 最高工资
	 * @param salaryUnit 工资单位
	 * @param experience 工作经验
	 * @param education 教育经历
	 * @return
	 */
	@GetMapping("/jobInfo")
	public Result selectAllJobs(@RequestParam("currentPage") Integer currentPage,
	                            @RequestParam(value = "pageSize", required = false) Integer pageSize,
	                            @RequestParam(value = "jobName", required = false) String jobName,
	                            @RequestParam(value = "hrName", required = false) String hrName,
	                            @RequestParam(value = "salaryMin", required = false) Short salaryMin,
	                            @RequestParam(value = "salaryMax", required = false) Short salaryMax,
	                            @RequestParam(value = "salaryUnit", required = false) String salaryUnit,
	                            @RequestParam(value = "experience", required = false) String experience,
	                            @RequestParam(value = "education", required = false) String education,
	                            @RequestParam(value = "deleted", required = false) Integer deleted,
	                            @RequestParam(value = "searchValue", required = false) String searchValue,
	                            @RequestParam(value = "companyId", required = false) String companyId,
	                            @RequestParam(value = "status", required = false) String status,
	                            @RequestParam(value = "companyOrHrId", required = false) String companyOrHrId) {
		Result result = null;
		try {
			List<String> statusList = new ArrayList<>();
			if (UtilValidate.isNotEmpty(status)) {
				statusList = Arrays.asList(status.split(","));
			}
			result = jobService.selectAllJobs(currentPage, pageSize, jobName, hrName,salaryMin,
											salaryMax, salaryUnit, experience, education, deleted,
											searchValue, companyId, statusList,companyOrHrId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("岗位 查询 失败");
		}

		return result;
	}

	/**
	 * 查询工作详情
	 * @param jobId 工作编号
	 * @param companyId 公司编号
	 * @return
	 */
	@GetMapping("/jobInfo/detail")
	public Result selectOneJob(@RequestParam("jobId") String jobId,
	                           @RequestParam("companyId") String companyId){
		Result result = null;
		try {
			if (UtilValidate.isEmpty(jobId) ||
					UtilValidate.isEmpty(companyId)) {
				result = Result.failed("参数为空");
			}else {
				result = jobService.selectOneJob(jobId, companyId);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("岗位详情 查询 失败");
		}
		return result;
	}

	/**
	 * 添加工作信息
	 * @param job 工作信息
	 * @return
	 */
	@PutMapping("/jobInfo")
	public Result addJob(@RequestBody Job job){
		Result result = null;
		try {
			if (UtilValidate.isEmpty(job)) {
				result = Result.failed("参数为空");
			}else {
				int insert = jobService.addJob(job);
				if (insert == 3){
					result = Result.succeed("岗位 添加 成功");
				}else if (insert == -2){
					result = Result.failed("岗位 添加 失败，岗位数已达上限，请升级会员");
				}else {
					result = Result.failed("岗位 添加 失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("岗位 添加 失败");
		}
		return result;
	}

	/**
	 * 修改工作信息
	 * @param job 工作信息
	 * @return
	 */
	@PostMapping("/jobInfo")
	public Result updateJob(@RequestBody Job job){
		Result result = null;
		try {
			if (UtilValidate.isEmpty(job)) {
				result = Result.failed("参数为空");
			}else {
				int update = jobService.updateJob(job, job.getNumber());
				if (update == 3){
					result = Result.succeed("岗位 修改 成功");
				}else if (update == -2){
					result = Result.failed("岗位 修改 失败，岗位数已达上限，请升级会员");
				}else {
					result = Result.failed("岗位 修改 失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("岗位 修改 失败");
		}
		return result;
	}

	/**
	 * 删除工作信息
	 * @param job 工作信息
	 * @return
	 */
	@DeleteMapping("/jobInfo")
	public Result deleteJob(@RequestBody Job job){
		Result result = null;
		try {
			if (UtilValidate.isEmpty(job.getJobId())) {
				result = Result.failed("参数为空");
			}else {
				if (UtilValidate.isEmpty(job.getDeleted())){
					job.setDeleted(Constants.getNoDel());
				}
				int update = jobService.deleteJob(job);
				if (update == 3){
					result = Result.succeed("岗位 删除 成功");
				}else {
					result = Result.failed("岗位 删除 失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("岗位 删除 失败");
		}
		return result;
	}

	@GetMapping("/getBookmarks")
	public Result getBookmarks(@RequestParam("userId") String userId){
		Result result = null;
		try {
			if (UtilValidate.isEmpty(userId)) {
				return Result.succeed(new ArrayList<>(), "用户编号为空");
			}
			User user = userService.getById(userId);
			String bookmarks = user.getBookmarks();
			if (UtilValidate.isEmpty(bookmarks)) {
				return Result.succeed(new ArrayList<>(), "收藏为空");
			}

			List<String> bookMarkList = Arrays.asList(bookmarks.split(","));
			result = Result.succeed(bookMarkList);

		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("查询收藏的岗位失败");
		}
		return result;
	}

	@PostMapping("/bookmarkJob")
	public Result bookmarkJob(@RequestParam(value = "userId") String userId,
	                            @RequestParam(value = "jobId") String jobId){
		Result result = null;
		try {
			User user = userService.getById(userId);
			String bookmarks = user.getBookmarks();
			if (UtilValidate.isNotEmpty(bookmarks)) {
				bookmarks += jobId+",";
			}else {
				bookmarks = jobId+",";
			}
			user.setBookmarks(bookmarks);
			boolean b = userService.updateById(user);
			if (b){
				result = Result.succeed("收藏成功");
			}else {
				result = Result.failed("收藏失败");
			}
		}catch (Exception e){
			e.printStackTrace();
			throw new OptionException("收藏岗位失败");
		}
		return result;
	}

	@PostMapping("/cancelBookmarkJob")
	public Result cancelBookmarkJob(@RequestParam(value = "userId") String userId,
	                                  @RequestParam(value = "jobId") String jobId){
		Result result = null;
		try {
			User user = userService.getById(userId);
			String bookmarks = user.getBookmarks();
			String replace = bookmarks.replace(jobId + ",", "");
			user.setBookmarks(replace);
			boolean b = userService.updateById(user);
			if (b){
				result = Result.succeed("取消收藏成功");
			}else {
				result = Result.failed("取消收藏失败");
			}
		}catch (Exception e){
			e.printStackTrace();
			throw new OptionException("取消收藏岗位失败");
		}
		return result;
	}

	@GetMapping("/getBookmarkList")
	public Result getBookmarkList(@RequestParam("currentPage")Integer currentPage,
	                                 @RequestParam(value = "pageSize", required = false)Integer pageSize,
	                                 @RequestParam(value = "searchValue", required = false)String searchValue,
	                                 @RequestParam(value = "userId") String userId){
		Result result = null;
		try {
			User user = userService.getById(userId);
			String bookmarks = user.getBookmarks();
			if (UtilValidate.isEmpty(bookmarks)) {
				return Result.succeed(null, "收藏为空");
			}

			List<String> bookMarkList = Arrays.asList(bookmarks.split(","));

			result = jobService.selectBookmarkList(currentPage, pageSize,
					searchValue, bookMarkList);

		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("查询收藏的岗位失败");
		}
		return result;
	}

	@GetMapping("/countJob")
	public Result countJob() {
		Result result = null;
		try {
			result = jobService.countJob();
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("查询失败");
		}
		return result;
	}

}
